package com.ymatou.iapi.godmch.testcase;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.addProviderBean;
import com.ymatou.iapi.godmch.parameter.applySupplierBean;
import com.ymatou.iapi.godmch.parameter.createIndepMchBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.service.addProviderCall;
import com.ymatou.iapi.godmch.service.applyAgentCall;
import com.ymatou.iapi.godmch.service.applySupplierCall;
import com.ymatou.iapi.godmch.service.createIndepMchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.mapping.godmch.supplierLogMapper;
import com.ymttest.database.model.country_info;
import com.ymttest.database.model.mch_info;
import com.ymttest.database.model.mch_relation;
import com.ymttest.database.model.godmch.supplierLog;
import com.ymttest.database.model.godmch.supplierLogExample;
import com.ymttest.database.sqlwapper.godmch.country_infoWapper;
import com.ymttest.database.sqlwapper.godmch.mch_infoWapper;
import com.ymttest.database.sqlwapper.godmch.mch_relationWapper;
import com.ymttest.database.sqlwapper.godmch.supplierLogWapper;
import com.ymttest.utils.TestDataManager;
/**
*申请成为供货商
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_applySupplier{
	private static applySupplierBean bean;
	private static applySupplierCall call;
	private static mch_infoWapper mchInfoWapper = new mch_infoWapper();
	private static mch_relationWapper relationWapper=new mch_relationWapper();
	country_infoWapper countryinfoWapper;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("申请成为供货商");
	}

	@Before
	public void caseUp() {
		bean=new applySupplierBean();
		call=new applySupplierCall();
		countryinfoWapper=new country_infoWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	/**
	 * 校验独立商户 和 供货商
	 * @param mchId
	 * @param supplierId
	 * @throws InterruptedException
	 */
	private void check_mchInfo(Integer mchId,Integer supplierId) throws InterruptedException{
		Logger.comment("验证数据库mch_info表");
		//独立商户
		Map<String, Object> map = new HashMap();
		map.put("mchId", mchId);
		mch_info  mchinfo=mchInfoWapper.selectby(map).get(0);
		//供货商
		Map<String, Object> supplier_map = new HashMap();
		supplier_map.put("mchId", supplierId);
		mch_info  supplier_mchinfo=mchInfoWapper.selectby(supplier_map).get(0);
		Logger.verifyEquals(0, mchinfo.getAgent(), "验证独立商户的Agent");
		Logger.verifyEquals(1, mchinfo.getChannels(), "验证独立商户的channels");
		Logger.verifyEquals(supplier_mchinfo.getMchShareType(), mchinfo.getMchShareType(), "验证独立商户的MchShareType");
		Logger.verifyEquals(0, mchinfo.getState(), "验证独立商户的State");
		Logger.verifyEquals(0, supplier_mchinfo.getAgent(), "验证供货商的Agent");
		Logger.verifyEquals(1, supplier_mchinfo.getChannels(), "验证供货商的channels");
		Logger.verifyEquals(mchinfo.getMchShareType(), supplier_mchinfo.getMchShareType(), "验证证供货商的MchShareType");
		Logger.verifyEquals(-2, supplier_mchinfo.getState(), "验证证供货商的State=-2待审核");
		//独立商户 新增 20200326
		Logger.verifyEquals(2, supplier_mchinfo.getIndepMchFlg(), "验证数据库mch_info中的IndepMchFlg独立商户标识(1:独立商户主商户 2:独立商户供货商 3:独立商户代理商)");
		
		
		//修改 20200409 独立小程序默认是自由支付
		Logger.verifyEquals(5, supplier_mchinfo.getPayMode(), "验证数据库mch_info中的paymode=5 自有支付");
        //默认开通domesticPayMode=1 盛付通-海鲲科技
        Logger.verifyEquals(1, supplier_mchinfo.getDomesticPayMode(), "验证数据库mch_info中的DomesticPayMode");
        //商户所在的国家如果不支持本国币种结算，落地USD，否则落地对应国家币种
        country_info countryinfo=countryinfoWapper.selectByPrimaryKey(supplier_mchinfo.getCountry());
        if(countryinfo.getSupportLocalCurrency().equals(0))
        	Logger.verifyEquals("USD", supplier_mchinfo.getCurrency(), "验证数据库mch_info中的Currency");
        else
        	Logger.verifyEquals(countryinfo.getCurrency(), supplier_mchinfo.getCurrency(), "验证数据库mch_info中的Currency");
        
	}
	/**
	 * 独立商户与供货商关系表
	 * @param mchId
	 * @param supplierId
	 * @throws InterruptedException
	 */
	private void check_relation(Integer mchId,Integer supplierId) throws InterruptedException
	{
		//独立商户与供货商关系表
		Logger.comment("验证数据库mch_relation表");
		//申请供货商关系时，独立商户是代理商，供货商是渠道商身份
		mch_relation relation=relationWapper.selectByPrimaryKey(mchId, supplierId).get(0);
		Logger.verifyEquals(-2, relation.getAgentType(), "验证AgentType");
		Logger.verifyEquals(-2, relation.getAgentModel(), "验证AgentModel");
		Logger.verifyEquals(-2, relation.getState(), "验证state");
	}
	/**
	 * 校验供货商日志
	 * @param mchId
	 * @param supplierId
	 * @param supplierLogNum
	 * @throws InterruptedException
	 */
	private void  check_supplierLog(Integer mchId,Integer supplierId,Integer supplierLogNum) throws InterruptedException {
		//供货商日志
		Logger.comment("验证数据库supplierLog表");
		supplierLogExample supplierLogExample=new supplierLogExample();
		supplierLogWapper supplierLogWapper=new supplierLogWapper();
		supplierLogExample.createCriteria().andMchIdEqualTo(mchId).andSupplierIdEqualTo(supplierId).andLogTypeEqualTo(1);
		List<supplierLog> supplierLogs=  supplierLogWapper.selectByExample(supplierLogExample);
		Logger.verifyEquals(supplierLogNum, supplierLogs.size(), "验证supplierLog条数");
	}
	/**
	 * 校验
	 * @param mchId
	 * @throws JSONException
	 * @throws InterruptedException
	 */
	private void check_verify(Integer mchId,Integer supplierLogNum) throws JSONException, InterruptedException
	{
		Integer supplierId=call.getSupplierId();
		Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
		Logger.verifyEquals(true, supplierId>0, "验证SupplierId");
		Logger.verifyEquals(true, call.getAccessToken().length()>0, "验证AccessToken");
		
		check_mchInfo(mchId, supplierId);
		check_relation(mchId, supplierId) ;
		check_supplierLog(mchId,supplierId,supplierLogNum);
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_applySupplier_001() {
		Logger.start(true,"正例-申请成为供货商-商户Id是无代理独立商户-成功");
		try {
			
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
            createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
            
			bean=GodmchManageCallService.initApplySupplier(createIndepMchCall.getmchId());
			call.setData(bean);
			call.callService();			
			
			check_verify(createIndepMchCall.getmchId(),1);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_applySupplier_002() {
		Logger.start(true,"正例-申请成为供货商-申请成为供货商-商户Id是有代理独立商户-成功");
		try {
			
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
            createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
            GodmchManageCallService.applyAndapproveAgent(createIndepMchCall.getmchId());

			bean=GodmchManageCallService.initApplySupplier(createIndepMchCall.getmchId());
			call.setData(bean);
			call.callService();			
			
			check_verify(createIndepMchCall.getmchId(),1);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_applySupplier_003() {
		Logger.start(true,"正例-申请成为供货商-申请成为供货商-商户Id是独立商户的供货商-失败");
		try {
			//1：创建独立商户
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
            createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
            Thread.sleep(5000);//休眠目的是为了创建独立商户，把对应的配置  同步到用户那边，然后用户 拿到配置再创建供货商
            //2:独立商户的供货商
			bean=GodmchManageCallService.initApplySupplier(createIndepMchCall.getmchId());
			call=GodmchManageCallService.applySupplierCall(bean);
			
			GodmchManageCallService.approveSupplier(createIndepMchCall.getmchId(),call.getSupplierId());

			bean=GodmchManageCallService.initApplySupplier(call.getSupplierId());
			
			call.setData(bean);
			call.callService();			

			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_applySupplier_004() {
		Logger.start(true,"正例-申请成为供货商-申请成为供货商-商户Id是独立商户的代理-成功");
		try {
			//1：创建独立商户
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
            createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
            Thread.sleep(5000);//休眠目的是为了创建独立商户，把对应的配置  同步到用户那边，然后用户 拿到配置再创建代理
            //2:独立商户的代理商
            applyAgentCall applyAgentCall=GodmchManageCallService.applyAndapproveAgent(createIndepMchCall.getmchId());

            bean=GodmchManageCallService.initApplySupplier(applyAgentCall.getAgentId());
			
			call.setData(bean);
			call.callService();		
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_applySupplier_005() {
		Logger.start(true,"反例-申请成为供货商-申请成为供货商-商户Id是平台纯渠道商-失败");
		try {
			//创建渠道
			Integer mchId_channl = GodmchManageCallService.CreateSellerMch();

			bean=GodmchManageCallService.initApplySupplier(mchId_channl);
					
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_applySupplier_006() {
		Logger.start(true,"反例-申请成为供货商-商户Id是平台纯代理商-失败");
		try {
			//创建渠道
			Integer mchId_channl = GodmchManageCallService.CreateSellerMch();
            //纯代理
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			List<createmchBean> createmchList=GodmchManageCallService.InitcreatemchBeanForAgent(mchId_channl, userId_agent);
			GodmchManageCallService.createmchCallForAgent(createmchList.toArray(new
					createmchBean[createmchList.size()]));
			createmchList.get(0).setCountry("CHN");
            Integer mchId_Agent=userId_agent;

			bean=GodmchManageCallService.initApplySupplier(mchId_Agent);
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			 
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_applySupplier_007() {
		Logger.start(true,"反例-申请成为供货商-商户Id是平台渠道商+代理商-失败");
		try {
			//创建渠道
			Integer mchId_channl = GodmchManageCallService.CreateSellerMch();
            //渠道商+代理商
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			List<createmchBean> createmchList=GodmchManageCallService.InitcreatemchBeanForAgent(mchId_channl, userId_agent);
			GodmchManageCallService.createmchCallForAgent(createmchList.toArray(new
					createmchBean[createmchList.size()]));
			createmchList.get(0).setCountry("USA");
            Integer mchId_Agent=userId_agent;

			bean=GodmchManageCallService.initApplySupplier(mchId_Agent);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			 
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_applySupplier_008() {
		Logger.start(true,"反例-申请成为供货商-商户Id是服务商-失败");
		try {
	        //创建服务商
			addProviderBean  addProviderBean = GodmchManageCallService.InitAddProviderBean();
			addProviderCall  addProviderCall=GodmchManageCallService.InitAddProviderCall(addProviderBean);
            Integer mchId_channl=addProviderBean.getProviderId();

            bean=GodmchManageCallService.initApplySupplier(mchId_channl);   
            
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_applySupplier_009() {
		Logger.start(true,"反例-申请成为供货商-所属国家是CHN-失败");
		try {
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
            createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);

			bean=GodmchManageCallService.initApplySupplier(createIndepMchCall.getmchId());
			bean.setCountry("CHN");
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("供货商不支持中国大陆身份注册"), "验证Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_applySupplier_010() {
		Logger.start(true,"反例-申请成为供货商-mchId不存在-失败");
		try {
	    	bean.setMchId(999999999);
			bean.setCountry("USA");
			bean.setMobile(TestDataManager.getPhoneNum());
			bean.setPassword(TestDataManager.getRandomNum(6));
			bean.setAppSource(TestDataManager.getRandomIntNum(6));
			bean.setTerminalSource(TestDataManager.getRandomIntNum(6));
			bean.setAppKey("godmch.iapi.ymatou.com");
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("5004", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("商户不存在"), "验证Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_applySupplier_011() {
		Logger.start(true,"反例-申请成为供货商-mchId为空-失败");
		try {
	    	//bean.setMchId();
			bean.setCountry("USA");
			bean.setMobile(TestDataManager.getPhoneNum());
			bean.setPassword(TestDataManager.getRandomNum(6));
			bean.setAppSource(TestDataManager.getRandomIntNum(6));
			bean.setTerminalSource(TestDataManager.getRandomIntNum(6));
			bean.setAppKey("godmch.iapi.ymatou.com");
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_applySupplier_012() {
		Logger.start(true,"反例-申请成为供货商-country为空-失败");
		try {
			
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
            createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);

        	bean.setMchId(createIndepMchCall.getmchId());
    		//bean.setCountry("USA");
    		bean.setMobile(TestDataManager.getPhoneNum());
    		bean.setPassword(TestDataManager.getRandomNum(6));
    		bean.setAppSource(TestDataManager.getRandomIntNum(6));
    		bean.setTerminalSource(TestDataManager.getRandomIntNum(6));
    		bean.setAppKey("godmch.iapi.ymatou.com");
    		
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_applySupplier_013() {
		Logger.start(true,"反例-申请成为供货商-mobile为空-失败");
		try {
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
            createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);

        	bean.setMchId(createIndepMchCall.getmchId());
    		bean.setCountry("USA");
    		//bean.setMobile(TestDataManager.getPhoneNum());
    		bean.setPassword(TestDataManager.getRandomNum(6));
    		bean.setAppSource(TestDataManager.getRandomIntNum(6));
    		bean.setTerminalSource(TestDataManager.getRandomIntNum(6));
    		bean.setAppKey("godmch.iapi.ymatou.com");
    		
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_applySupplier_014() {
		Logger.start(true,"反例-申请成为供货商-password为空-失败");
		try {
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
            createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);

        	bean.setMchId(createIndepMchCall.getmchId());
    		bean.setCountry("USA");
    		bean.setMobile(TestDataManager.getPhoneNum());
    		//bean.setPassword(TestDataManager.getRandomNum(6));
    		bean.setAppSource(TestDataManager.getRandomIntNum(6));
    		bean.setTerminalSource(TestDataManager.getRandomIntNum(6));
    		bean.setAppKey("godmch.iapi.ymatou.com");
    		
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_applySupplier_015() {
		Logger.start(true,"反例-申请成为供货商-appSource为空-失败");
		try {
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
            createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);

        	bean.setMchId(createIndepMchCall.getmchId());
    		bean.setCountry("USA");
    		bean.setMobile(TestDataManager.getPhoneNum());
    		bean.setPassword(TestDataManager.getRandomNum(6));
    		//bean.setAppSource(TestDataManager.getRandomIntNum(6));
    		bean.setTerminalSource(TestDataManager.getRandomIntNum(6));
    		bean.setAppKey("godmch.iapi.ymatou.com");
    		
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_applySupplier_016() {
		Logger.start(true,"反例-申请成为供货商-terminalSource为空-失败");
		try {
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
            createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);

        	bean.setMchId(createIndepMchCall.getmchId());
    		bean.setCountry("USA");
    		bean.setMobile(TestDataManager.getPhoneNum());
    		bean.setPassword(TestDataManager.getRandomNum(6));
    		bean.setAppSource(TestDataManager.getRandomIntNum(6));
    		//bean.setTerminalSource(TestDataManager.getRandomIntNum(6));
    		bean.setAppKey("godmch.iapi.ymatou.com");
    		
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_applySupplier_017() {
		Logger.start(true,"反例-申请成为供货商-appKey为空-失败");
		try {
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
            createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);

        	bean.setMchId(createIndepMchCall.getmchId());
    		bean.setCountry("USA");
    		bean.setMobile(TestDataManager.getPhoneNum());
    		bean.setPassword(TestDataManager.getRandomNum(6));
    		bean.setAppSource(TestDataManager.getRandomIntNum(6));
    		bean.setTerminalSource(TestDataManager.getRandomIntNum(6));
    		//bean.setAppKey("godmch.iapi.ymatou.com");
    		
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_applySupplier_018() {
		Logger.start(true,"正例-申请成为供货商-商户Id是无代理独立商户-成功");
		try {
			//20200508 供货商、代理商与主商户一致
			for(int i=1;i<=4;i++)
			{
				createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
				createIndepMchBean.setMchShareType(i);//1小程序2h5 3：独立小程序 4：独立h5
	            createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
	            
				bean=GodmchManageCallService.initApplySupplier(createIndepMchCall.getmchId());
				call.setData(bean);
				call.callService();			
			
				check_verify(createIndepMchCall.getmchId(),1);
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}